package cn.yoyo.controller.cons;

import cn.yoyo.entity.BlogContent;
import cn.yoyo.entity.Favourite;
import cn.yoyo.entity.Like;
import cn.yoyo.service.BlogService;
import cn.yoyo.service.FavouriteService;
import cn.yoyo.service.LikeService;
import cn.yoyo.tools.MySession;
import cn.yoyo.tools.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/api/cons/blog")
public class ConsBlogController {
    @Autowired
    BlogService blogService;
    @Autowired
    LikeService likeService;
    @Autowired
    FavouriteService favouriteService;

    @RequestMapping("/go2PublishBlog")
    public String go2PublishBlog(){
        return "blog/publishBlog";
    }

    @RequestMapping("/go2FindMyBlogs")
    public String go2FindMyBlogs(){
        return "blog/myBlogs";
    }

    @RequestMapping("/go2MyLikes")
    public String go2MyLikes(){
        return "blog/myLikes";
    }

    @RequestMapping("/findMyLikes")
    @ResponseBody
    public Map<String,Object> findMyLikes(){
        return RespJson.pack(200,"",blogService.findMyLikes());
    }

    @RequestMapping("/go2MyFavourites")
    public String go2MyFavourites(){
        return "blog/myFavourites";
    }

    @RequestMapping("/findMyFavourites")
    @ResponseBody
    public Map<String,Object> findMyFavourties(){
        return RespJson.pack(200,"",blogService.findMyFavourites());
    }

    @RequestMapping("/findMyBlogs")
    @ResponseBody
    public Map<String,Object> findMyBlogs(){
        return RespJson.pack(200,"",blogService.findMyBlogs(MySession.getUserId()));
    }


    @RequestMapping("/publishBlog")
    @ResponseBody
    public Map<String,Object> publishBlog(@RequestBody BlogContent blogContent){
        blogContent.getBlog().setCreateTime(new Date());
        blogContent.getBlog().setUserId(MySession.getUserId());
        blogContent.getBlog().setNickname(MySession.getNickname());
        blogService.add(blogContent);
        return RespJson.pack(200,"发表成功,等待审核",blogService.findAllCanSee());
    }

    @RequestMapping("/like/{blogId}")
    @ResponseBody
    public Map<String,Object> like(@PathVariable("blogId")String blogId){
        likeService.add(blogId);
        return RespJson.pack(200,"点赞成功",blogService.findById(blogId));
    }
    @RequestMapping("/delLike")
    @ResponseBody
    public Map<String,Object> delLike(@RequestBody Like like){
        System.out.println(like.toString());
        likeService.delete(like);
        return RespJson.pack(200,"取消点赞成功",blogService.findById(like.getBlogId()));
    }

    @RequestMapping("/favourite/{blogId}")
    @ResponseBody
    public Map<String,Object> favourite(@PathVariable("blogId")String blogId){
        favouriteService.add(blogId);
        return RespJson.pack(200,"收藏成功",blogService.findById(blogId));
    }
    @RequestMapping("/delFavourite")
    @ResponseBody
    public Map<String,Object> delFavourite(@RequestBody Favourite favourite){
        favouriteService.delete(favourite);
        return RespJson.pack(200,"取消收藏成功",blogService.findById(favourite.getBlogId()));
    }
    @RequestMapping("/go2Update")
    public String go2Update(){
        return "blog/updateBlog";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody BlogContent blogContent){
        blogContent.getBlog().setCreateTime(new Date());
        blogContent.getBlog().setUserId(MySession.getUserId());
        blogContent.getBlog().setNickname(MySession.getNickname());
        blogService.update(blogContent);
        return RespJson.pack(200,"修改成功,等待审核",blogService.findAllCanSee());
    }

    @RequestMapping("/deleteById/{blogId}")
    @ResponseBody
    public Map<String,Object> deleteById(@PathVariable("blogId")String blogId){
        // 删除后 将该用户的博客数据返回给前端
        blogService.delete(blogId);
        return RespJson.pack(200,"删除成功",blogService.findMyBlogs(MySession.getUserId()));
    }
}
